<template>
	<view>
		<view class="wrap">
			<view class="order-header">
				<view class="u-tabs-box">
					<u-tabs-swiper activeColor="#10beca" ref="tabs"
						:list="list" :current="current" @change="change" :is-scroll="false" swiperWidth="750">
					</u-tabs-swiper>
				</view>
				<!-- <view class="u-p-30 bg-white">
					<u-search v-model="key" :show-action="false" shape="square" clearabled @change="searchKeyword"></u-search>
				</view> -->
				<list-filter-panel :filterType="filterType" @filter-change="filterChange"></list-filter-panel>
			</view>
			<view class="grid-box" v-for="(res, index) in orderList" :key="index">
				<navigator class="order" @click="details(res)">
					<view class="top u-border-bottom u-p-b-20">
						<view class="left">
							<view class=""><text class="u-tips-color">{{res.number}}</text></view>
							<view v-if="res.state == '已入库'"><text class="u-font-24 u-tips-color">由<text class="bold-txt">{{res.staffname}}</text>操作</text></view>
						</view>
						<view class="right">
							<text v-if="res.state == '已入库'" class="color-success">{{ res.state }}</text>
							<text v-else-if="res.state == '已取消'" class="color-danger">{{ res.state }}</text>
							<text v-else>{{ res.state }}</text>
						</view>
					</view>
					<view class="u-p-t-20">
						<view class="address">
							<!-- <u-icon name="map-fill" size="32"></u-icon> -->
							<text class="u-font-32">[入库站点]{{res.storename}}</text>
						</view>
						<!-- <view class="u-p-t-8" v-if="res.state == '已入库'">
							<view class="u-flex u-font-24">
								<text class="u-text-right">操作店员：{{res.staffname}}</text>
							</view>
						</view> -->
					</view>
					
					<view class="middle" v-if="res.state == '待入库'">
						<view class="u-flex" style="justify-content: space-between;">
							<view class="">
								<text class="label u-tips-color">待入库重量</text>
								<view class="u-m-t-8">
									<text class="u-flex-1 u-text-right u-font-36">{{res.userorderweight}}</text>
									<text class="unit">KG</text>
								</view>
							</view>
							<view class="">
								<text class="label u-tips-color">待入库数量</text>
								<view class="u-m-t-8">
									<text class="u-flex-1 u-text-right u-font-36">{{res.userorderquantity}}</text>
									<text class="unit">件</text>
								</view>
							</view>
							<view class="">
								<text class="label u-tips-color">待入库金额</text>
								<view class="u-m-t-8">
									<text class="u-flex-1 u-text-right u-font-36">{{res.userorderamount}}</text>
									<text class="unit">元</text>
								</view>
							</view>
						</view>
					</view>
					
					<view class="middle" v-if="res.state == '已入库'">
						<view class="u-flex" style="justify-content: space-between;">
							<view class="">
								<text class="label u-tips-color">入库重量</text>
								<view class="u-m-t-8">
									<text class="u-flex-1 u-text-right u-font-36">{{res.orderweight}}</text>
									<text class="unit">KG</text>
								</view>
							</view>
							<view class="">
								<text class="label u-tips-color">入库数量</text>
								<view class="u-m-t-8">
									<text class="u-flex-1 u-text-right u-font-36">{{res.orderquantity}}</text>
									<text class="unit">件</text>
								</view>
							</view>
							<view class="">
								<text class="label u-tips-color">入库金额</text>
								<view class="u-m-t-8">
									<text class="u-flex-1 u-text-right u-font-36">{{res.orderamount}}</text>
									<text class="unit">元</text>
								</view>
							</view>
						</view>
					</view>
					
					<view class="bottom u-border-top u-p-t-20" v-if="res.storagetime">
						<view class="more">
							<view class="u-flex-1">
								<text class="u-font-24 u-tips-color">{{res.storagetime}}</text>
							</view>
							<u-icon name="clock" class="u-tips-color"></u-icon>
							<text class="u-font-24 u-tips-color">{{$u.timeFrom(new Date(res.storagetime), false)}}入库</text>
						</view>
					</view>
				</navigator>
			</view>
			<view class="u-p-30">
				<u-loadmore :status="loadStatus"></u-loadmore>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters,
		mapActions
	} from 'vuex';
	
	export default {
		props:{
			sendStoreId:{
				type: [Number,String],
				default: 0
			},
			filterType:{
				type: Array,
				default:()=>['time']
			}
		},
		data() {
			return {
				list: [
					{
						value: '',
						name: '全部'
					},
					{
						value: 0,
						name: '待入库'
					},
					{
						value: 1,
						name: '已入库'
					},
					{
						value: 2,
						name: '已取消'
					}
				],
				current: 1,
				swiperCurrent: 1,
				tabsHeight: 0,
				dx: 0,
				loadStatus: 'loadmore',
				PageIndex: 1,
				PageSize: 20,
				noMore: false,
				key: '',
				orderList: [],
				// 时间类型
				timeType: 0, //筛选时间类别：0全部，1今日，2昨日，3本月，4上月，5本季度，6上季度，7本年，9自定义时间段
				startDate: '', //开始时间
				endDate: '', //结束时间
				// 其他筛选条件
				filterStoreId: 0,
			}
		},
		computed: {
			...mapState({
				// storeId: state => state.storeId
			})
		},
		onLoad(options) {
		 //  if(options.current>0 || options.current == 0 ){
		 //  	this.current = options.current
			// this.swiperCurrent = options.current
		 //  }
		},
		mounted() {
			this.orderList = []
			this.PageIndex = 1
			this.getOrderList();
		},
		onPullDownRefresh() {
			this.pullDownRefresh()
		},
		onReachBottom(e) {
			this.reachBottom()
		},
		methods: {
			pullDownRefresh(){
				this.orderList = []
				this.PageIndex = 1
				this.getOrderList().then(() => {
					uni.stopPullDownRefresh(); //停止当前页面下拉刷新
				})
			},
			filterChange(obj){
				console.log('filterChange',obj)
				// for(let key in obj){
				// 	this.$set(this,key,obj[key])
				// }
				this.timeType = obj.timeType
				this.startDate = obj.startDate
				this.endDate = obj.endDate
				this.filterStoreId = obj.filterStoreId
				this.key = obj.key
				this.getOrderList(true);
			},
			// 查看详情
			details(item){
				if(item.ordertype == "小哥入库单"){
					uni.navigateTo({
						url: '/pages/shop/income/views/views?id='+item.id
					})
				}else{
					uni.navigateTo({
						url: '/pages/shop/income/user/views/views?id='+item.id
					})
				}
				
			},
			reachBottom() {
				// 此tab为空数据
				this.PageIndex += 1
				if (!this.noMore) {
					this.getOrderList().then((row) => {
						if (row.length == 0) {
							this.noMore = true
						}
					})
				}
			},
			// 页面数据
			getOrderList(reload = false) {
				if(reload){
					this.PageIndex = 1
					this.orderList = []
				}
				this.loadStatus = "loading"
				
				let state = ''
				if(this.current == 0){
					state = ''
				}else{
					state = this.list[this.current].value
				}
				
				return this.$u.post('/Store/InOrder/List', {
					"timeType": this.timeType,
					"startDate": this.startDate,
					"endDate": this.endDate,
					"storeId": this.sendStoreId || this.filterStoreId,
					"PageIndex": this.PageIndex,
					"PageSize": this.PageSize,
					"key": this.key,
					"state": state,
					"orderType": 1 //0全部，1.小哥入库单，2.线下单
					// "order": [
					//     {
					// 		"column": "createddatetime",
					// 		"isDesc": true
					//     }
					// ],
				}).then((res) => {
					if (res.rows.length > 0) {
						res.rows.forEach((item) => {
							item.re_weight = 0
							item.re_number = 0
							// 累加重量与件数
							// item.details.map((cell)=>{
							// 	if(cell.isweighing){
							// 		item.re_weight += cell.weight
							// 	}else{
							// 		item.re_number += cell.weight
							// 	}
							// })
							this.orderList.push(item)
						})
						if (res.rows.length < this.PageSize) {
							this.loadStatus = "nomore"
						} else {
							this.loadStatus = "loadmore"
						}
					} else {
						this.loadStatus = "nomore"
					}
					
					return res.rows
				})
			},
			// tab栏切换
			change(index) {
				this.PageIndex = 1;
				this.current = index;
				this.orderList = [];
				this.noMore = false;
				this.getOrderList();
			},
			searchKeyword(val){
				this.key = val
				this.PageIndex = 1;
				this.orderList = [];
				this.getOrderList();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}
	
	.swiper-box {
		flex: 1;
	}
	
	.swiper-item {
		height: 100%;
	}
	
	.order {
		// width: 710rpx;
		// background-color: #ffffff;
		// margin: 20rpx auto;
		// border-radius: 20rpx;
		// box-sizing: border-box;
		// padding: 20rpx;
		// font-size: 28rpx;
	
		.top {
			display: flex;
			justify-content: space-between;
			align-items: center;
	
			.left {
				// display: flex;
				align-items: center;
	
				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					display: flex;
					align-items: center;
				}
			}
	
			.right {
				color: $u-type-warning-dark;
			}
		}
	
		.item {
			display: flex;
			margin: 20rpx 0 0;
			justify-content: space-between;
	
			.left {
				margin-right: 20rpx;
	
				image {
					width: 160rpx;
					height: 160rpx;
					border-radius: 10rpx;
				}
			}
	
			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
					font-weight: bold;
				}
	
				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}
			}
	
			.right {
				margin-left: 10rpx;
				text-align: right;
	
				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}
	
				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}
	
		.total {
			text-align: right;
			font-size: 24rpx;
	
			.total-price {
				font-size: 32rpx;
				color: $u-color-orange;
			}
		}
	
		.middle {
			font-size: 24rpx;
			margin: 20rpx 0;
			line-height: 40rpx;
			.label{
				
			}
			.unit{
				font-size: 24rpx;
				// color: $u-tips-color;
			}
		}
	
		.bottom {
			// display: flex;
			margin-top: 20rpx;
			// justify-content: space-between;
			// align-items: center;
			.more{
				display: flex;
				justify-content: center;
				align-items: center;
			}
			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
			}
			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}
</style>
